from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

import time
from fake_useragent import UserAgent
from lxml import etree
import re
import os
import random
import logging

from gt_cookies import open_cookie_file
from information import USER,PASSWORD

logger = logging.getLogger(__name__)
fh = logging.FileHandler('kuaipai.log')
sh = logging.StreamHandler()
logger.addHandler(fh)
logger.addHandler(sh)
logger.setLevel(logging.DEBUG)

def get_driver(ip_port):
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--window-size=1366,768')
    chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])#去掉正在被自动化检测程序控制的提示
    chrome_options.add_argument('user-agent={}'.format(UserAgent().random))
    chrome_options.add_argument('--proxy-server=http://{}'.format(ip_port))
    # chrome_options.add_argument('--headless')
    driver = webdriver.Chrome(executable_path='chromedriver1.exe', chrome_options=chrome_options)
    return driver

class KuaiPai:
    def __init__(self,ip_port,keyword):
        self.driver = get_driver(ip_port)
        self.keyword = keyword
        self.ip_port = ip_port

    def get_interger(self, data): #转换数字
        try:
            res = int(data)
        except:
            try:
                res = float(data)
            except:
                res = data
        return res

    def is_empty(self, data):  # 处理数据为空的字段 str转int float
        if isinstance(data, list):
            if data != []:
                jieguo = data[0].strip()
                jieguo = self.get_interger(jieguo)
            else:
                jieguo = ''
        else:
            jieguo = self.get_interger(data)
        return jieguo

    def take_cookie(self):  #添加cookies
        cookies_list_1 = open_cookie_file('all2_cookies.json', 'a')
        cookies_list_2 = open_cookie_file('all_cookies.json', 'b')
        self.driver.get("http://www.baidu.com")
        # 删除第一次建立连接时的cookie
        self.clear_cookie()
        # 读取登录时存储到本地的cookie
        while True:
            if random.randint(1, 101) % 2 == 0:
                con = random.choice(cookies_list_1)
            else:
                con = random.choice(cookies_list_2)
            lis = con.split(';')
            try:
                for i in lis:
                    i = i.split('=',1)
                    self.driver.add_cookie({
                        'domain': '.baidu.com',  # 此处xxx.com前，需要带点
                        'name': i[0],
                        'value': i[-1],
                        'path': '/',
                        'expires': None
                    })
                #     listCookies = json.loads(f.read())
                self.driver.refresh()
                break
            except Exception:
                logger.info('此cookie失效，已被记录！')
                with open('shixiao_coo.json','a') as f:
                    f.write(con+'\n')

    def clear_cookie(self):
        self.driver.delete_all_cookies()

    # connect : 宽带拨号
    def connect(self,name, username, password):
        cmd_str = "rasdial %s %s %s" % (name, username, password)
        os.system(cmd_str)
        time.sleep(5)

    # disconnect : 断开宽带连接
    def disconnect(self,name):
        cmd_str = "rasdial %s /disconnect" % name
        os.system(cmd_str)
        time.sleep(5)

    def open_baidu(self,keyword):
        self.driver.get("http://www.baidu.com")
        # self.driver.add_cookie(self.cookie)
        # num = random.randint(1,3)
        # print(num)
        # self.driver.maximize_window()
        time.sleep(3)
        self.driver.find_element_by_xpath("//input[@id='kw']").send_keys(keyword)
        time.sleep(4)
        baidu = self.driver.find_element_by_xpath("//input[@id='su']")
        baidu.click()
        self.driver.implicitly_wait(7)
        time.sleep(3)
        # driver.quit()

    def take_baidu_login(self,user,password):
        self.driver.get('https://www.baidu.com')
        wait = WebDriverWait(self.driver, 10)
        time.sleep(1)
        wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="u1"]/a[2]'))).click() #点击登录
        # self.driver.switch_to.frame("TANGRAM__PSP_4__foreground") # 切换子窗口
        time.sleep(1.5)
        wait.until(EC.presence_of_element_located((By.ID, 'TANGRAM__PSP_11__footerULoginBtn'))).click()
        time.sleep(1.5)
        wait.until(lambda x:x.find_element_by_id("TANGRAM__PSP_11__userName")).send_keys(user) #user输入框
        time.sleep(2)
        wait.until(lambda x:x.find_element_by_id("TANGRAM__PSP_11__password")).send_keys(password) #user输入框
        time.sleep(1)
        wait.until(lambda x:x.find_element_by_id("TANGRAM__PSP_11__submit")).click() #点击登录
        time.sleep(10)
        item =  self.driver.get_cookies()
        print(item)

    def make_baidu_page(self):
        result = self.driver.find_elements_by_xpath('//*[@id="content_left"]/div[@class="result c-container "]/h3/a')
        for i in result:
            time.sleep(3)
            i.click()

    def request_jiezi(self,host_url,name_url): #截止模式
        # self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="content_left"]/div[last()]/h3/a')))
        # self.driver.switch_to.window(handles[-1]) #切换到刚打开的窗口
        target_url_id = []
        res_divs = self.driver.find_elements_by_xpath('//*[@id="content_left"]/div[@class="result c-container "]')
        content = self.driver.page_source
        res,name = '',''
        for div in res_divs:
            target_id = div.get_attribute('id')
            time_now = time.strftime("%H:%M:%S", time.localtime())
            url = self.my_xpath(content,'//div[@id="{}"]/div/div[2]/div[2]/a[1]/text()'.format(target_id))
            if not url:
                url = self.is_empty(self.my_xpath(content, '//div[@id="{}"]/div[2]/a[1]/text()'.format(target_id)))
            if not url:
                name = self.is_empty(
                    self.my_xpath(content, '//div[@id="{}"]/div/div[2]/div[2]/a[1]/span/text()'.format(target_id)))
                if not name:
                    name = self.is_empty(self.my_xpath(content, '//div[@id="{}"]/div[2]/a[1]/span/text()'.format(target_id)))
            else:
                name = ''
                res = re.findall(r'{}'.format(host_url),url[0])
                if not res:
                    res = re.findall(r'{}'.format(host_url.replace('www.','')), url[0])
            if res or name == name_url:  #这段代码可以封装
                time.sleep(3)
                WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@id="{}"]/h3/a'.format(target_id)))).click()
                time.sleep(random.randint(3,10))
                if random.randint(1,101)%3==0:
                    handles = self.driver.window_handles  # 窗口句柄
                    time.sleep(2)
                    self.driver.switch_to.window(handles[-1])  # 切换到新打开的窗口
                    time.sleep(random.randint(20,50))
                    self.driver.refresh()
                target_url_id.append(target_id)
                logger.info('{ip:'+self.ip_port+',目标域名:'+self.is_empty(res)+','+'熊掌号name:'+name+','+'百度关键词排名:'+target_id+','+'点击状态:'+'截止模式目标网址点击完成,'+'time:'+time_now+'}')
                return target_url_id
            else:
                if random.randint(1, 100) % 3 == 0:
                    time.sleep(2)
                    WebDriverWait(self.driver, 10).until(
                        EC.presence_of_element_located((By.XPATH, '//div[@id="{}"]/h3/a'.format(target_id)))).click()
                    handles = self.driver.window_handles  # 窗口句柄
                    time.sleep(random.randint(2,6))
                    self.driver.switch_to.window(handles[0])  # 切换到最初打开的窗口
                    time.sleep(random.randint(1,4))
                    logger.info('{ip:'+self.ip_port+','+'百度搜索关键词:'+self.keyword+','+'点击状态:'+'截止模式非目标网址点击完成,'+'time:'+time_now+'}')

    def request_jingguo(self,host_url,name_url): #经过模式
        target_url_host = []
        res_divs = self.driver.find_elements_by_xpath('//*[@id="content_left"]/div[@class="result c-container "]')
        content = self.driver.page_source
        res,name = '',''
        for div in res_divs:
            target_id = div.get_attribute('id')
            time_now = time.strftime("%H:%M:%S", time.localtime())
            url = self.my_xpath(content, '//div[@id="{}"]/div/div[2]/div[2]/a[1]/text()'.format(target_id))
            if not url:
                url = self.is_empty(self.my_xpath(content, '//div[@id="{}"]/div[2]/a[1]/text()'.format(target_id)))
            if not url:
                name = self.is_empty(self.my_xpath(content, '//div[@id="{}"]/div/div[2]/div[2]/a[1]/span/text()'.format(target_id)))
                if not name:
                    name = self.is_empty(self.my_xpath(content, '//div[@id="{}"]/div[2]/a[1]/span/text()'.format(target_id)))
            else:
                name = ''
                res = re.findall(r'{}'.format(host_url),url[0])
                if not res:
                    res = re.findall(r'{}'.format(host_url.replace('www.','')), url[0])
            # print(res)
            if res or name == name_url:  #这段代码可以封装
                time.sleep(random.randint(3,5))
                WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//div[@id="{}"]/h3/a'.format(target_id)))).click()
                time.sleep(random.randint(5, 8))
                handles = self.driver.window_handles  # 窗口句柄
                if random.randint(1, 101) % 3 == 0:
                    time.sleep(2)
                    self.driver.switch_to.window(handles[0])  # 切换到最初打开的窗口
                    time.sleep(random.randint(4,10))
                else:
                    time.sleep(random.randint(5,10))
                logger.info('{ip:'+self.ip_port+',目标域名:'+self.is_empty(res)+','+'熊掌号name:'+name+','+'百度关键词排名:'+target_id+','+'点击状态:'+'经过模式目标网址点击完成,'+'time:'+time_now+'}')
            else:
                if random.randint(1,101)%5 == 0:
                    time.sleep(random.randint(2, 4))
                    WebDriverWait(self.driver, 10).until(
                        EC.element_to_be_clickable((By.XPATH, '//div[@id="{}"]/h3/a'.format(target_id)))).click()
                    handles = self.driver.window_handles  # 窗口句柄
                    time.sleep(random.randint(4,6))
                    self.driver.switch_to.window(handles[0])  # 切换到最初打开的窗口
                    time.sleep(random.randint(3,6))
                    logger.info('{ip:'+self.ip_port+','+'百度搜索关键词:'+self.keyword+','+'点击状态:'+'经过模式非目标网址点击完成,'+'time:'+time_now+'}')
        return target_url_host

    def request_zhijie(self,host_url,name_url): #直接模式
        target_url_host = []
        res_divs = self.driver.find_elements_by_xpath('//*[@id="content_left"]/div[@class="result c-container "]')
        content = self.driver.page_source
        res,name = '',''
        for div in res_divs:
            target_id = div.get_attribute('id')
            time_now = time.strftime("%H:%M:%S", time.localtime())
            url = self.my_xpath(content, '//div[@id="{}"]/div/div[2]/div[2]/a[1]/text()'.format(target_id))
            if not url:
                url = self.is_empty(self.my_xpath(content, '//div[@id="{}"]/div[2]/a[1]/text()'.format(target_id)))
            if not url:
                name = self.is_empty(self.my_xpath(content, '//div[@id="{}"]/div/div[2]/div[2]/a[1]/span/text()'.format(target_id)))
                if not name:
                    name = self.is_empty(self.my_xpath(content, '//div[@id="{}"]/div[2]/a[1]/span/text()'.format(target_id)))
                # print(name)
            else:
                name = ''
                res = re.findall(r'{}'.format(host_url),url[0])
                if not res:
                    res = re.findall(r'{}'.format(host_url.replace('www.','')), url[0])
            if res or name == name_url:  #这段代码可以封装
                time.sleep(random.randint(2, 4))
                WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@id="{}"]/h3/a'.format(target_id)))).click()
                target_url_host.append(target_id)
                if random.randint(1,101)%3 == 0:
                    handles = self.driver.window_handles  # 窗口句柄
                    time.sleep(2)
                    self.driver.switch_to.window(handles[-1]) #切换到最初打开的窗口
                    time.sleep(50)
                    self.driver.refresh()
                logger.info('{ip:'+self.ip_port+',目标域名:'+self.is_empty(res)+','+'熊掌号name:'+name+','+'百度关键词排名:'+target_id+','+'点击状态:'+'直接模式目标网址点击完成,'+'time:'+time_now+'}')
                break
        return target_url_host

    def click_next_page(self,func,host_url,name_url,page,number=0):
        #number 限制回调次数
        shu = number
        shu += 1
        host_url_s = host_url
        name_url_s = name_url
        for i in range(page,10):
            try:
                res = func(host_url_s, name_url_s)
                if res:
                    return res
                if func == self.request_zhijie:
                    time.sleep(random.randint(5,15))
                else:
                    time.sleep(random.randint(1, 3))
                next_page = self.driver.find_element_by_xpath('//*[@id="page"]/div/a[last()]')
                next_page.click()
                time.sleep(5)
            except:
                if shu <= 15:
                    logger.info('点击访问失败，将重新访问！')
                    self.click_next_page(func,host_url_s,name_url_s,i,shu)
                    break
                else:
                    raise Exception('ip有问题')

    def exit_chrome(self):
        time.sleep(50)
        self.driver.quit()

    def my_xpath(self,data,ruler): #xpath打包方法
        return etree.HTML(data).xpath(ruler)

    def run(self,host_url,ch_host):
        rank = ''
        # self.take_baidu_login(USER, PASSWORD)
        self.driver.get('http://httpbin.org/get') #测试ip
        # time.sleep(5)
        if random.randint(1,101) <= 50:
            self.take_cookie()
        self.open_baidu(self.keyword)
        # self.make_baidu_page()
        time.sleep(5)
        # self.request_next_page('www.zhms.cn','九州醉')
        if random.randint(1, 1001) <= 700:
            rank = self.click_next_page(self.request_zhijie,host_url, ch_host, 0)
        elif 700 < random.randint(1, 1001) <= 900:
            self.click_next_page(self.request_jingguo,host_url, ch_host, 0)
        else:
            self.click_next_page(self.request_jiezi,host_url, ch_host, 0)
        self.exit_chrome()
        return rank
if __name__ == '__main__':
    kp = KuaiPai('','西红柿的功效与作用')
    kp.run('mip.ys991.com','991')
    # kp.take_cookie()

